package cn.yunxiang.summer.system.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.lock.annotation.Lock4j;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import cn.yunxiang.summer.common.domain.R;
import cn.yunxiang.summer.framework.exception.ServiceException;
import cn.yunxiang.summer.common.idempotent.annotation.RepeatSubmit;
import cn.yunxiang.summer.common.log.annotation.Log;
import cn.yunxiang.summer.common.log.enums.BusinessType;
import cn.yunxiang.summer.common.tenant.helper.TenantHelper;
import cn.yunxiang.summer.system.domain.bo.SysReferenceBo;
import cn.yunxiang.summer.system.domain.vo.SysReferenceVO;
import cn.yunxiang.summer.system.service.ISysTenantService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 团长管理
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/reference")
@ConditionalOnProperty(value = "tenant.enable", havingValue = "true")
public class SysReferenceController extends BaseController {

    private final ISysTenantService tenantService;



    /**
     * 新增团长租户
     */
//    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
//    @SaCheckPermission("system:reference:add")
    @Log(title = "新增团长", businessType = BusinessType.INSERT)
    @Lock4j
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody SysReferenceBo bo) {
        if (!tenantService.checkCompanyNameUnique(null,bo.getPersonName())) {
            return R.fail("新增团长'" + bo.getPersonName() + "'失败，团长名称已存在");
        }
        return toAjax(TenantHelper.ignore(() -> tenantService.insertByBo(tenantService.convert(bo))));
    }

    /**
     * 修改团长租户
     */
//    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
//    @SaCheckPermission("system:reference:edit")
    @Log(title = "修改团长", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/update")
    public R<Void> edit(@Validated @RequestBody SysReferenceBo bo) {
        Assert.notNull(bo.getId(), () -> new ServiceException("id不可为空"));
        Assert.notNull(bo.getId(), () -> new ServiceException("tenantId不可为空"));
        tenantService.checkTenantAllowed(bo.getTenantId());
        if (!tenantService.checkCompanyNameUnique(bo.getTenantId(), bo.getPersonName())) {
            return R.fail("修改团长'" + bo.getPersonName() + "'失败，团长名称已存在");
        }
        return toAjax(tenantService.updateByBo(tenantService.convert(bo)));
    }


    /**
     * 查询团长列表
     * @param query
     * @return
     */
//    @SaCheckRole(value = {
//        TenantConstants.SUPER_ADMIN_ROLE_KEY,
//        TenantConstants.TENANT_ADMIN_ROLE_KEY
//    }, mode = SaMode.OR)
//    @SaCheckPermission("system:tenant:list")
    @PostMapping("/list")
    public R<IPage<SysReferenceVO>> list(@RequestBody SysReferenceBo query){
        return R.ok(tenantService.getReferenceList(query));
    }






}
